<?php
//车辆排期控制器
namespace Management\Controller;

use Think\Controller;

use Myself\FileSystem;

class CardateController extends BaseController {



	protected $m = NULL;

	protected $m_class = NULL;

	protected $m_config = NULL;

    protected $statusArr=array("未认证","已认证");

	

	public function _initialize(){

		parent::_initialize();

		$this->m = M('order');
		$this->car = M('car');

	}
	//车辆排期表（订单中的boktime和driver_end）
    public function index(){

		header('Content-Type:text/html;charset=utf-8');
        $start = date('Y-m-d').' 00:00:01';
        $end   = date('Y-m-d').' 23:59:59';
		$where['car_id']=['neq',''];
        $where['booktime'] = array('between',[$start,$end]);//只暂时当天记录2020/10/10 王鑫提出
		//-----------------搜索-------------------



		//-------------------------搜索end-----------------

		$pageno = I('p',1,'int');

		$pageCount = 10;

		$list = $this->m->where($where)->page($pageno.','.$pageCount)->order('id desc')->select();
        // echo $this->m->getLastsql();die;
		if(!empty($list)){
			foreach ($list as $k => $v) {
                $list[$k]['car_no'] = $this->car->where(['car_id'=>$v['car_id']])->getField('car_no');
                $list[$k]['booktime'] =substr($v['booktime'],10,10);
				$list[$k]['driver_end'] =substr($v['driver_end'],10,10);
			}
		}

		$this->assign('list',$list);// 赋值数据集

		$count = $this->m->where($where)->count();// 查询满足要求的总记录数

		$this->assign('count',$count);// 赋值分页输出	

		$Page = new \Think\Page($count,$pageCount);// 实例化分页类 传入总记录数和每页显示的记录数

		$show = $Page->showManage();// 分页显示输出

		$this->assign('page',$show);// 赋值分页输出

        //-------------------------固定参数输出-----------------
		$this->display('index');
    }

	public function edit(){
		if(I('id')){
			$id = I('id',0,'int');
			$order = $this->m->find($id);
			$this->assign('order',$order);
		}
		$this->assign('back_url',$_SERVER['HTTP_REFERER']);
		$this->display('edit');

    }
    //修改预计结束时间
    public function save(){
    	$temp = $this->m->create();
    	if($temp['id']){
    		$res = $this->m->where(['id'=>$temp['id']])->save($temp);
    	}
    	if($res){
		    $this->selectPage_ajax( array('act' => '修改', 'back_url' =>U('Cardate/index'),'jump_url' => U('Cardate/edit',['id'=>$temp['id']])));
		}else{
			 $this->selectPage_ajax( array('act' => '操作未', 'back_url' => U('Cardate/index'),'jump_url' => U('Cardate/edit',['id'=>$temp['id']])));
		}

    }
     public function gante(){
     	$car = M('car')->field(['car_no'])->order('car_id asc')->select();
     	// ll($car);die;
     	$this->assign('car',$car);
    	$this->display('gante');
     }
    //甘特图
    public function GanteData(){
    	$start = date("Y-m-d",time()).' 00:00:00';
		$end = date("Y-m-d",time()).' 23:59:59';
		$where['booktime'] = array('between',array($start,$end));
    	$car = M('car')->where('1=1')->select();
    	$arr=[];
    	if(!empty($car)){
    		foreach ($car as $k => $v) {
    			$data =[];
				$where['car_id'] = array('eq',$v['car_id']);
    			$list = $this->m->field(['booktime','driver_end','car_id'])->where($where)->select();
    			// ll($list);
    			if(!empty($list)){
    				foreach ($list as $kk => $vv) {
    					$car_no = $this->car->where(['car_id'=>$vv['car_id']])->getField('car_no');
    					$data[$kk]['colorNum'] = $kk;
    					$data[$kk]['endTime'] = $vv['driver_end'];
    					$data[$kk]['startTime'] = $vv['booktime'];
    					$data[$kk]['item'] = $car_no;
    					$data[$kk]['quantity'] = 2000;
    				}
    			}
    			// ll($data);die;
    			$arr[$k]['list'] = $data;
    			$arr[$k]['plant'] = $v["car_no"];
    		}
    	}
    	// die;
    	// ll($arr);
    	// die;
    	$this->ajaxReturn(['arr'=>$arr,'start'=>$start,'end'=>$end]);exit;
    	
    }
    //新甘特图
    public function newGanteData(){
    	$start = date("Y-m-d",time()).' 05:00:00';
		$end = date("Y-m-d",time()).' 22:00:00';
		$where['booktime'] = array('between',array($start,$end));
    	$car = M('car')->where('1=1')->order('car_id asc')->select();
    	$arr=[];
    	if(!empty($car)){
    		foreach ($car as $k => $v) {
    			$data =[];
				$where['car_id'] = array('eq',$v['car_id']);
    			$list = $this->m->field(['booktime','driver_end','car_id'])->where($where)->select();
    			// ll($list);
    			if(!empty($list)){
    				foreach ($list as $kk => $vv) {
    					$car_no = $this->car->where(['car_id'=>$vv['car_id']])->getField('car_no');
    					$data[$kk]['value'] = '有排期';
    					$data[$kk]['end'] = str_replace("-","/",$vv['driver_end']);
    					$data[$kk]['start'] = str_replace("-","/",$vv['booktime']);;
    				}
    			}
    			// ll($data);die;
    			$arr[$k]['innerData']= $data;
    		}
    	}
    	// die;
    	// ll($arr);
    	// die;
    	$this->ajaxReturn(['arr'=>$arr]);exit;
    	
    }

	

	

	

	

}